<?php

namespace Admin\Controller;

use Org\Nx\Data;
use Org\Nx\Page;


class MessageController extends BaseController
{
    public function index()
    {
        $page = I('get.p');
        $typename = '';
        $status = '';

        if (IS_POST) {
            $typename = I('post.typename');
            $status = I('post.status');
        }
        $this->assign('typename', $typename);
        $this->assign('status', $status);

        if ($typename != '') {
            $where['typeName'] = $typename;
        }
        if ($status != '') {
            $where['status'] = $status;
        }

        $list = M('message')
            ->alias('a')
            ->field('a.*')
            ->order('a.status asc,a.created_at desc')
            ->where($where)
            ->page($page, $this->pageSize)
            ->select();


        $count = M('message')
            ->alias('a')
            ->field('a.*')
            ->where($where)
            ->count();
        $page = new Page($count, $this->pageSize);
        $page->rollPage = $this->rollPage;

        $this->assign('list', $list);
        $this->assign('page', $page->show());

        $this->display();
    }

    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');
            $data['updated_at'] = time();
            $result = M('message')->where(array('id' => $data['id']))->save($data);
            if (false === $result) {
                $this->error('修改失败');
            } else {
                $this->success('修改成功', U('/Admin/Message/'));
            }
        } else {
            $id = I('id');
            $article = M('message')->where(array('id' => $id))->find();
            $this->assign('article', $article);
            $this->display();
        }
    }

}